package tv.acgn.advertisement.publish.dao.dsl;

import java.util.List;
import java.util.Optional;

import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tv.acgn.advertisement.publish.po.AdPlanPO;
import tv.acgn.advertisement.publish.po.QAdPlanPO;
import tv.acgn.advertisement.publish.vo.adplan.AdPlanRequestVO;

/**
 * @program : advertisement-ad-publish-dao
 * @description : AdPlan 用于复杂参数持久化的操作类
 * @author : junG
 * @date : 2019-05-03 01:32
 **/

@Service
public class AdPlanDslDao {

    @Autowired
    private JPAQueryFactory queryFactory;

    /**
     * 根据查询参数 查询用户推广计划
     * @param adPlanRequestVO [1]
     *
     * @return {@link Optional<List<AdPlanPO>>}
     *
     * @author junG
     * @description 根据查询参数 查询用户推广计划
     * @date 0:13 2019/5/2
     */
    public Optional<List<AdPlanPO>> findByQueryParam(AdPlanRequestVO adPlanRequestVO) {
        QAdPlanPO qAdPlanPO = QAdPlanPO.adPlanPO;
        List<AdPlanPO> adPlanPOList = queryFactory.selectFrom(qAdPlanPO)
            .where(
                qAdPlanPO.planName.contains(adPlanRequestVO.getPlanName())
                    .and(qAdPlanPO.planStatus.eq(adPlanRequestVO.getPlanStatus()))
                    .and(qAdPlanPO.startDate.after(adPlanRequestVO.getStartDate()))
                    .and(qAdPlanPO.endDate.before(adPlanRequestVO.getEndDate()))
                    .and(qAdPlanPO.createTime.after(adPlanRequestVO.getCreateTime()))
                    .and(qAdPlanPO.updateTime.before(adPlanRequestVO.getUpdateTime()))
            )
            .fetch();

        return Optional.ofNullable(adPlanPOList);
    }

}
